package com.itheima.edu.info.maneger.dao;

import com.itheima.edu.info.maneger.controller.BaseStudentController;
import com.itheima.edu.info.maneger.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao {
    //1.创建Student学生数组长度为5
    private static ArrayList<Student> stus = new ArrayList<>();
    //使用静态代码块进行初始化
    static {
        Student student = new Student("0001", "张三", 23, "1991");
        Student student2 = new Student("0002", "李四", 24, "1996");
        Student student3 = new Student("0003", "王五", 25, "1992");
        Student student4 = new Student("0004", "赵六", 23, "1994");

        stus.add(student);
        stus.add(student2);
        stus.add(student3);
        stus.add(student4);

    }
    //查找所有学生
    public Student[] findAllStudent() {
        Student[] student = new Student[stus.size()];
        for (int i = 0; i < stus.size(); i++) {
            student[i] = stus.get(i);
        }
        return student;
    }


    //添加学生
    public boolean addStudent(Student student) {
     stus.add(student);
     return true;
    }
    //删除学生
    public void deleteStudent(String id) {
        int index = getIndex(id);
        stus.remove(index);
    }
    //获取id
    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < stus.size(); i++) {
            Student student = stus.get(i);
            if (student != null && student.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    //修改学生
    public void updateStudent(String updateId, Student newstudent) {
        int index = getIndex(updateId);
        stus.set(index,newstudent);
    }
}













